import { ResConfig } from "../SysConfig/SysConfig";
import ResManage from "./ResManage";



const { ccclass, property } = cc._decorator;

@ccclass
export default class SoundManage extends cc.Component {

    public static Instance: SoundManage = null;

    //声音开关
    private m_isMusicMute: boolean = false;
    private m_isSoundMute: boolean = false;

    onLoad() {
        if (SoundManage.Instance == null) {
            SoundManage.Instance = this;
        }
        else {
            this.destroy();
            return;
        }
    }

    start() {

    }

    /**播放背景音乐 */
    public playBgMusic(musicPath: string, isLoop: boolean) {
        let clip = ResManage.Instance.getAssetFomeCache(ResConfig.SoundBundle, musicPath, cc.AudioClip);
        if (clip) {
            let audioID = cc.audioEngine.playMusic(clip, isLoop);
            let volume = (this.m_isMusicMute) ? 0 : 1.0;
            cc.audioEngine.setVolume(audioID, volume);
        }
    }
    /**停止背景音乐 */
    public stopBgMusic(): void {
        cc.audioEngine.stopMusic();
    }

    /**播放音效 */
    public playSound(soundPath: string) {
        if (this.m_isSoundMute === true) {
            return;
        }

        let clip = ResManage.Instance.getAssetFomeCache(ResConfig.SoundBundle, soundPath, cc.AudioClip);
        if (clip) {
            cc.audioEngine.playEffect(clip, false);
            let volume = (this.m_isSoundMute) ? 0 : 1.0;
            cc.audioEngine.setEffectsVolume(volume);
        }
    }

    /**设置背景音乐开关 */
    public setMusicMute(isMute: boolean): void {
        this.m_isMusicMute = isMute;
    }
    /**设置音效开关 */
    public setSoundsMute(isMute: boolean): void {
        this.m_isSoundMute = isMute;
    }

}
